/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AUDIO_WAVEFORM_DECODER_H
#define AUDIO_WAVEFORM_DECODER_H
#include "capbilities/include/audio_decoder.h"
#include "capbilities/include/demuxer.h"
#include "common/sample_info.h"
#include <mutex>
#include <memory>
#include <atomic>
#include <ohaudio/native_audiostream_base.h>
#include <thread>


class Decode {
public:
    Decode() = default;
    ~Decode();

    int32_t Init(SampleInfo &sampleInfo);
    int32_t Start();
    void StartRelease();

private:
    void AudioDecInputThread();
    void AudioDecOutputThread();
    void Release();
    void ReleaseThread();
    int32_t CreateAudioDecoder();
    
    std::unique_ptr<AudioDecoder> audioDecoder_ = nullptr;
    std::unique_ptr<Demuxer> demuxer_ = nullptr;

    std::mutex mutex_;
    std::mutex doneMutex;
    std::atomic<bool> isStarted_{false};
    std::atomic<bool> isReleased_{false};
    std::atomic<bool> isAudioDone_{false};
    std::unique_ptr<std::thread> audioDecInputThread_ = nullptr;
    std::unique_ptr<std::thread> audioDecOutputThread_ = nullptr;
    std::condition_variable doneCond_;
    SampleInfo sampleInfo_;
    CodecUserData *audioDecContext_ = nullptr;
    // 这些指针保持为nullptr，因为我们不再创建音频渲染器
    OH_AudioStreamBuilder *builder_ = nullptr;
    OH_AudioRenderer *audioRenderer_ = nullptr;
};

#endif // AUDIO_WAVEFORM_DECODER_H